package com.loong.mapper.impl;

import com.loong.mapper.AddressMapper;
import com.loong.pojo.Address;
import com.loong.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.List;

public class AddressMapperImpl implements AddressMapper {
    Logger logger = Logger.getLogger(AddressMapperImpl.class);
    @Override
    public List<Address> selectAddressByUid(int uid) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
            AddressMapper mapper = sqlSession.getMapper(AddressMapper.class);
            return mapper.selectAddressByUid(uid);
        }catch (Exception e){
            logger.error(e.getMessage());
            return null;
        }
    }

    @Override
    public void insertAddress(Address address) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
            AddressMapper mapper = sqlSession.getMapper(AddressMapper.class);
            mapper.insertAddress(address);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }

    @Override
    public void deleteAddressByAid(int aid) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            AddressMapper mapper = sqlSession.getMapper(AddressMapper.class);
            mapper.deleteAddressByAid(aid);
            sqlSession.commit();
        }
    }

    @Override
    public void updateAddressToCommon() {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            AddressMapper mapper = sqlSession.getMapper(AddressMapper.class);
            mapper.updateAddressToCommon();
            sqlSession.commit();
        }
    }

    @Override
    public void updateAddressToDefault(int aid) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            AddressMapper mapper = sqlSession.getMapper(AddressMapper.class);
            mapper.updateAddressToDefault(aid);
            sqlSession.commit();
        }
    }

    @Override
    public void updateAddress(Address address) {
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            AddressMapper mapper = sqlSession.getMapper(AddressMapper.class);
            mapper.updateAddress(address);
            sqlSession.commit();
        }
    }
}
